#include "regist_wid.h"
#include "ui_regist_wid.h"

regist_wid::regist_wid(QTcpSocket *socket,QWidget *parent) :
    QWidget(parent),re_tcp(socket),
    ui(new Ui::regist_wid)
{
    ui->setupUi(this);
    setMinimumSize(800,600);
}

regist_wid::~regist_wid()
{
    delete ui;
}
void regist_wid::on_black_clicked()
{
    regwid_closed();
    qDebug()<<"注册点击返回";
    this->close();
}

void regist_wid::on_regist_clicked()
{
//获取密码用户名并进行tcp通信判断

QString name= ui->lineEdit->text();
qDebug()<<"name:"<<name;
QString pswd= ui->lineEdit_2->text();
qDebug()<<"pswd:"<<pswd;
QString pswd2= ui->lineEdit_3->text();
qDebug()<<"pswd2:"<<pswd2;

if (name.isEmpty() || pswd.isEmpty()) {
    QString error;
    if (name.isEmpty()) error += "• 用户名不能为空\n";
    if (pswd.isEmpty()) error += "• 密码不能为空";

    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setText("以下问题需要修正：");
    msgBox.setInformativeText(error);
    msgBox.exec();
    return;
}
if(pswd!=pswd2)
{
    QString error;
    error="密码不一致，请重试";
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.setText("以下问题需要修正：");
    msgBox.setInformativeText(error);
    msgBox.exec();
    return;

}
re_tcp->write("R ");
re_tcp->write(name.toLocal8Bit());
re_tcp->write(" ");
re_tcp->write(pswd.toLocal8Bit());

TcpDataReceiver recevier(re_tcp,this);
recevier.setReadMode(TcpDataReceiver::ReadFixedLength, 2);//固定两字节
QByteArray data2 = recevier.readData();
qDebug()<<"respense2:"<<data2;
         if (!data2.isEmpty())  {  // 确保至少有1字节数据
              if (data2[0] == 'Y') {
                  qDebug() << "操作成功";
                  QMessageBox::information(this, "注册", "注册成功");
                  regwid_closed();
                  this->close();//返回登录页面
                  return;
              } else if(data2[0]=='N'){
                  if(data2[1]=='E'){
                      QMessageBox::information(this, "注册",QString("用户已存在"));
                      return ;
                  }else{
                          QMessageBox::information(this, "注册",QString("失败"));

                    return ;
                      }
                  }
              }
}

